<template>
  <div>
    <div class="flex justify-between h-full">
      <div class="w-2/3 pr-2">
        <a-card class="w-full">
          <div class="flex justify-between mb-3">
            <a-input v-model:value="formState.pyjm" class="w-1/3" placeholder="项目编号/项目名称/助记码" @keydown.enter="queryList" @blur="queryList" />
            <BaseButton type="primary" class="" :disabled="!medicaments.bh || !surgery.icd9Code" @click="saveData"> 对照</BaseButton>
          </div>
          <!-- <div>
          <BaseExport type="运维" class="mb-3" name="临床诊断" code="00001185" :search-params="formState">导出</BaseExport>
            </div> -->
          <BaseTable
            :loading="loading"
            column-code="00001196"
            :data-source="tableData"
            bordered
            fill
            :custom-row="(record) => ({ onClick: () => (medicaments = record), onDblclick: () => ((medicaments = record), saveData()) })"
            :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
            :row-class-name="(record) => (record.bh === medicaments.bh ? 'table-row-new' : '')"
            @change="onPageChange"
          >
            <template #bodyCell="{ column, index, record }">
              <template v-if="column.dataIndex === 'xh'">
                {{ index + 1 }}
              </template>
              <template v-if="column.dataIndex === 'yxzt'">
                <a-tag v-if="record.ssdm" color="green">已对照</a-tag>
                <a-tag v-else color="blue">未对照</a-tag>
              </template>
              <template v-if="column.dataIndex === 'xmmc'">
                <MedicineInfo :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" />
              </template>
            </template>
          </BaseTable>
        </a-card>
      </div>
      <div class="w-1/3">
        <a-card class="flex-1">
          <a-input-search v-model:value="searchValue" placeholder="手术代码/手术名称/助记码" @search="getSurgeryData" />
          <BaseTable
            :data-source="surgeryData"
            bordered
            fill
            :columns="columns"
            :loading="surgeryLoading"
            class="mt-3"
            :row-class-name="(record) => (record.id === surgery.id ? 'table-row-new' : '')"
            :custom-row="(record) => ({ onClick: () => (surgery = record), onDblclick: () => ((surgery = record), saveData()) })"
          >
            <template #bodyCell="{ column, index }">
              <template v-if="column.dataIndex === 'xh'">
                {{ index + 1 }}
              </template>
            </template>
          </BaseTable>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'

const loading = ref(false)
const tableData = ref([])
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const surgeryLoading = ref(false)
const surgeryData = ref([])
const columns = ref([
  {
    dataIndex: 'xh',
    title: '序号'
  },
  {
    dataIndex: 'icd9Code',
    title: '手术代码'
  },
  {
    dataIndex: 'icd9Name',
    title: '手术名称'
  }
])
const medicaments = ref({})
const surgery = ref({})
const searchValue = ref('')
const formState = ref({})
const getSurgeryData = async () => {
  surgeryLoading.value = true
  const { code, data } = await post('/common/masterData/getDictICD9List', {
    zjm: searchValue.value,
    status: 1,
    pageNum: 1,
    pageSize: 20
  })

  surgeryLoading.value = false
  if (code !== 0) return
  surgeryData.value = data
}

const saveData = async () => {
  const { code, msg } = await post('/operation/opex/doSaveICD9SFXM', { sfxmbBh: medicaments.value.bh, ssdm: surgery.value.icd9Code })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  queryList()
}
const queryList = async () => {
  loading.value = true
  // formState.value
  const { code, data, total } = await post('/operation/opex/getDrugInfoList', {
    zjm: formState.value.pyjm,
    xmlb: ['6'],
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  loading.value = false
  if (code !== 0) return
  tableData.value = data
  page.value.total = total
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}
onMounted(() => {
  queryList()
  getSurgeryData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
